﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Data;
using System.IO.IsolatedStorage;

namespace WeiboBulletin
{
    using WeiboSdk;
    using WeiboBulletin.Model;
    using UpdateTileAgent;
    using System.IO;    

    public partial class LoginPage : PhoneApplicationPage
    {
        #region ProgressIndicatorIsVisibleProperty
        public static readonly DependencyProperty ProgressIndicatorIsVisibleProperty =
            DependencyProperty.Register("ProgressIndicatorIsVisible",
            typeof(bool),
            typeof(LoginPage),
            new PropertyMetadata(false));

        public bool ProgressIndicatorIsVisible
        {
            get { return (bool)GetValue(ProgressIndicatorIsVisibleProperty); }
            set { SetValue(ProgressIndicatorIsVisibleProperty, value); }
        }
        #endregion

        public LoginPage()
        {
            InitializeComponent();
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            SystemTray.ProgressIndicator = new ProgressIndicator();
            SystemTray.ProgressIndicator.Text = "waiting";

            Binding bindingData;
            bindingData = new Binding("ProgressIndicatorIsVisible");
            bindingData.Source = this;
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsVisibleProperty, bindingData);
            BindingOperations.SetBinding(SystemTray.ProgressIndicator, ProgressIndicator.IsIndeterminateProperty, bindingData);

            oAuthControl.ifSave = true;
            oAuthControl.OAuthBack = LoginCallback;

            oAuthControl.OAuthBrowserNavigating = new EventHandler(oauthBrowserNavigating);
            oAuthControl.OAuthBrowserNavigated = new EventHandler(oauthBrowserNavigated);
            oAuthControl.OAuthBrowserCancelled = new EventHandler(oauthBrowserCancelled);

        }

        public void LoginCallback(bool isSuccess, SdkAuthError authError, SdkAuthRes authResponse)
        {
            ProgressIndicatorIsVisible = false;

            if (isSuccess)
            {
                Setting setting = Setting.GetCurrentAppSetting();
                setting.UserID = authResponse.userId;
                setting.AccessToken = authResponse.acessToken;
                setting.AccessSecret = authResponse.acessTokenSecret;
                Setting.SetCurrentAppSetting(setting);

                System.Diagnostics.Debug.WriteLine(string.Format("userid:{0},accessToken:{1},accessTokenSecret:{2}",
                    authResponse.userId,
                    authResponse.acessToken,
                    authResponse.acessTokenSecret));
            }
            else
            {
                MessageBox.Show(
                    String.Format("Fail to login: {0} (code: {1}, special code: {2}).", authError.errMessage, authError.errCode, authError.specificCode),
                    "Error",
                     MessageBoxButton.OK);
            }

            this.NavigationService.GoBack();
        }

        private void oauthBrowserCancelled(object sender, EventArgs e)
        {
            ProgressIndicatorIsVisible = false;
            this.NavigationService.GoBack();
        }

        private void oauthBrowserNavigating(object sender, EventArgs e)
        {
            ProgressIndicatorIsVisible = true;
        }

        private void oauthBrowserNavigated(object sender, EventArgs e)
        {
            ProgressIndicatorIsVisible = false;
        }
    }
}